//
//  PayViewModel.swift
//  EyeVisionForPatients
//
//  Created by PMC on 2020/2/20.
//  Copyright © 2020 tao Tao. All rights reserved.
//

import UIKit
import HandyJSON

/// 订单类型
// 1-图文咨询 3-云门诊 4-处方
enum OrderType: Int {
    case advisory     = 1
    case cloud        = 3
    case prescription = 4
    case nurse = 5
}

struct PayOrderModel: HandyJSON {
    /// 订单编号
    var orderNumber: String?
    /// 诊断id
    var diagnoseId: String?
    /// 订单id
    var orderId: String?
}

struct PayViewModel {
    let order: PayOrderModel
    
    var orderNum: String { return order.orderNumber ?? "" }
    var orderId: String { return order.orderId ?? "" }
    var diagnoseId: String { return order.diagnoseId ?? "" }
    
    /// 生成订单
    static func generateOrder(doctorId: String,
                              patientId: String,
                              orderAmount: String,
                              orderType: OrderType = .advisory,
                              scheduleId: String = "",
                              prescriptionId: String = "",
                              businessType : String,
                              success: @escaping (_ model: PayOrderModel) -> Void,
                              failure: @escaping (_ errorMsg: String?) -> Void) {
        let paras: [String: Any] = [
            "doctorId": doctorId,
            "orderAmount": orderAmount,
            "patientId": patientId,
            "orderType": orderType.rawValue,
            "scheduleId": scheduleId,
            "businessType": businessType,
            "prescriptionId": prescriptionId
        ]
        HttpTool.shared.request(target: ApiManager.generateOrder(paras: paras), success: { (json, code) in
            switch code {
            case .failure:
                failure(json["message"].description)
            case .success:
                if let model = JSONDeserializer<PayOrderModel>.deserializeFrom(json: json["data"].description) {
                    success(model)
                } else {
                    failure("请求数据发生错误，稍候重试")
                }
            }
        }) { errorMsg in
            failure(errorMsg)
        }
    }
    

    static func queryOrderStatus(orderId: String, finished: @escaping (_ errorMsg: String?) -> Void) {
       // PayViewModel.queryOrderLoading(orderId: orderId, finished: finished)
        let paras = ["orderId": orderId]
        HttpTool.shared.request(target: ApiManager.queryPrepaidOrder(paras: paras), success: { (json, code) in
            switch code {
            case .failure:
                finished(json["message"].description)
            case .success:
                PayViewModel.queryOrderLoading(orderId: orderId, finished: finished)
            }
        }) { errorMsg in
            finished(errorMsg)
        }
    }
    /// 查询订单状态
    static func queryOrderLoading(orderId: String, finished: @escaping (_ errorMsg: String?) -> Void) {
        let paras = ["orderId": orderId]
        
        HttpTool.shared.request(target: ApiManager.queryOrderStatus(paras: paras), success: { (json, code) in
            switch code {
            case .failure:
                DispatchQueue.main.asyncAfter(deadline: .now() + 2) {
                    PayViewModel.queryOrderLoading(orderId: orderId, finished: finished)
                }
           //     finished(json["message"].description)
            case .success:
                finished(nil)
            }
        }) { errorMsg in
            finished(errorMsg)
        }
    }
    
    
    
    
    /// 查询订单状态
    static func queryAppointmentOrderStatus(orderId: String, finished: @escaping (_ errorMsg: String?) -> Void) {
        let paras = ["orderId": orderId]
        HttpTool.shared.request(target: ApiManager.queryAppointmentOrderStatus(paras: paras), success: { (json, code) in
            switch code {
            case .failure:
                finished(json["message"].description)
            case .success:
                finished(nil)
            }
        }) { errorMsg in
            finished(errorMsg)
        }
    }
    
}
